package cn.dxh.stagefinal.controller;

import cn.dxh.stagefinal.bean.Business;
import cn.dxh.stagefinal.bean.Employee;
import cn.dxh.stagefinal.bean.Matter;
import cn.dxh.stagefinal.bean.Vehicle;
import cn.dxh.stagefinal.service.BusinessService;
import cn.dxh.stagefinal.service.MatterService;
import com.alibaba.fastjson.annotation.JSONField;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: Panjianwei
 * @date: 2019/12/10 11:45
 * 代码是一门艺术，需要我们细细用心品味！
 */
@Controller
public class BusinessController {
    @Autowired
private BusinessService businessService;
    //  1.  查询事项和车辆
    @Autowired
    private MatterService matterService;
    @RequestMapping("/finance/findMVAll")
    public String findVehicleAll(ModelMap map){
        List<Vehicle> vehicleAll = businessService.findVehicleAll();
        List<Matter> finall = matterService.getall();
        map.addAttribute("MatterList",finall);
        map.addAttribute("vehiclelist",vehicleAll);
        return "/finance/fin_add";
    }
//    2.添加数据
    @RequestMapping("/finance/addBusiness")
    public String addBusiness(Business business, HttpServletResponse response,HttpSession session)
    {

        int s = businessService.addBusiness(business,session);
        if(s>0)
        {
            try {
                response.setContentType("text/html;charset=utf-8");
                response.getWriter().print("<script>alert('添加成功');location.href='/finance/findMVAll';</script>");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            response.getWriter().print("<script>alert('添加失败');location.href='/finance/findMVAll';</script>");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
//  3.分页模查事项
    @RequestMapping("/finance/findMatterAll")
//    当前页码值和摸查条件
    public String findMatterAll(@RequestParam(defaultValue = "1")int pageindex, String itemName, ModelMap map)
    {

        PageInfo<Matter> allMatter = matterService.findAllMatter(pageindex, 5, itemName);

        map.addAttribute("pageList",allMatter);
        map.addAttribute("itemName",itemName);
        return "/finance/fin_list_matter";
    }
//    4。修改
//    (1)展示(主键查询)
    @RequestMapping("/finance/findbyitemsid")
    public String findbyitemsid(ModelMap map,int itemsid)
    {
        Matter findbyitemsid = matterService.findbyitemsid(itemsid);
        map.addAttribute("matter",findbyitemsid);
        return "/finance/find_update_matter";
    }
//    (2）修改
    @RequestMapping("/finance/updatematter")
    public String updatematter(Matter matter,HttpServletResponse response)
    {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer=null;
        int updatematter = matterService.updatematter(matter);
        String itemsname = matter.getItemsname();
        Integer itemsid = matter.getItemsid();
        System.out.println(itemsid);
        System.out.println(itemsname);
        try {
           writer = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(updatematter>0)
        {

            writer.print("<script>alert('添加成功');location.href='/finance/findMatterAll';</script>");
        }
        writer.print("<script>alert('新增失败');location.href='/finance/findMatterAll';<script>");
        return null;
    }
//    5.buinses表的分页和模糊查询
    @RequestMapping("/finance/findBusinessAll")
    public String findBusinessAll(@RequestParam(defaultValue = "-1") int itemsid,@RequestParam(defaultValue ="")String recorddate,@RequestParam(defaultValue ="")String relation,@RequestParam(defaultValue = "-1") int account,@RequestParam(defaultValue = "1") int pageindex,ModelMap map)
    {
        float sum=0,sum1=0;
        System.out.println(recorddate);
//        查询事项列表
        List<Matter> itemlist = matterService.getall();
        PageInfo<Business> businessList = businessService.findBusinessAll(itemsid, relation, recorddate, account, pageindex, 5);
        for (Business business : businessList.getList()) {
            if(business.getAccount()==0)
            {
                String money = business.getMoney();
                sum+= Float.parseFloat(money);
            }
            if(business.getAccount()==1)
            {
                String money = business.getMoney();
                sum1+= Float.parseFloat(money);
            }
        }
//        算出进出账总合计
        List<Float> math = businessService.math();
            map.addAttribute("summation",math.get(0));
            map.addAttribute("chuzhang",math.get(1));
        int pages = businessList.getNextPage();
//        存放进出账金额当前页面计
        map.addAttribute("sum",sum);
        map.addAttribute("sum1",sum1);
//      存放模查条件
        map.put("itemsid",itemsid);
        map.put("recorddate",recorddate);
        map.put("relation",relation);
        map.put("account",account);
        map.put("itemList",itemlist);
        map.put("pageinfo",businessList);
        return "finance/fin_list_business";
    }
//    6.busines表的修改
//    (1)主键查询(展示数据)
    @RequestMapping("/finance/findbybid")
    public String findbybid(int bid,ModelMap map)
    {
        Business business = businessService.findbybid(bid);
        List<Matter> getall = matterService.getall();
        List<Vehicle> vehicleList = businessService.findVehicleAll();
        map.addAttribute("matterlist",getall);
        map.addAttribute("vehiclelist",vehicleList);
        map.addAttribute("business",business);
        return "/finance/fin_update_business";
    }
//    (2)修改
    @RequestMapping("/finance/udpatebusiness")
    public String udpatebusiness( Business business, HttpServletResponse response)
    {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=null;
        try {
             out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        int udpatebusiness = businessService.udpatebusiness(business);
        if(udpatebusiness>0)
        {
            out.print("<script>alert('修改成功');location.href='/finance/findBusinessAll';</script>");
        }
        out.print("<script>alert('修改失败');location.href='/finance/findBusinessAll';</script>");

        return null;
    }
//    7.删除（修改状态值 ）
@RequestMapping("/finance/udpatestatu")
    public String updatestatu(HttpServletResponse response,int bid)
    {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        int updatestatu = businessService.updatestatu(bid);
        if(updatestatu>0)
        {
            out.print("<script>alert('删除成功');location.href='/finance/findBusinessAll';</script>");
        }
        out.print("<script>alert('删除失败');location.href='/finance/findBusinessAll';</script>");
        return null;
    }
//    交易确认
    @RequestMapping("/finance/updatecheck")
    public String updatecheck(int bid,HttpServletResponse response)
    {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        int updatecheck = businessService.updatecheck(bid);

        if(updatecheck>0)
        {
            out.print("<script>alert('确认成功');location.href='/finance/findBusinessAll';</script>");
        }
        out.print("<script>alert('确认失败');location.href='/finance/findBusinessAll';</script>");
        return null;

    }
//    跳转添加页面
    @RequestMapping("/finance/gotoUrl")
    public String gotoUrl()
    {
        return "finance/fin_addmatter";
    }
//添加事项：
    @RequestMapping("/finance/addmatter")
    public String addmater(String itemsname,HttpServletResponse response)
    {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        int i = matterService.addmater(itemsname);

        if(i>0)
        {
            out.print("<script>alert('添加成功');location.href='/finance/findMVAll';</script>");
        }
        out.print("<script>alert('添加失败');location.href='/finance/findMVAll';</script>");
        return null;
    }
}
